package org.gvsig.raster.osm.swing.impl;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.gvsig.i18n.Messages;

/**
 * Panel to add OSM layers
 * @author Nacho Brodin (nachobrodin@gmail.com)
 */
public class AddServerPanel extends JPanel {
	private static final long    serialVersionUID = 1L;
	private JTextField           textFormat       = null;
	private JTextField           textName         = null;
	private JTextField           textURL          = null;
	private JTextField           textLevels       = null;
	private String               separator        = null;
	
	public AddServerPanel(String separator) {
		this.separator = separator;
		init();
	}
	
	private void init() {
		GridBagConstraints gridBagConstraints;
		setLayout(new GridBagLayout());
		setBorder(BorderFactory.createTitledBorder(Messages.getText("server")));
		
		gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.fill = GridBagConstraints.NONE;
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.insets = new Insets(2, 2, 2, 2);
		add(new JLabel(Messages.getText("name")), gridBagConstraints);
		
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 1;
		add(new JLabel(Messages.getText("url")), gridBagConstraints);
		
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 2;
		add(new JLabel(Messages.getText("levels")), gridBagConstraints);
		
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 3;
		add(new JLabel(Messages.getText("format")), gridBagConstraints);
		
		gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.weightx = 1.0;
		add(getJTextFieldName(), gridBagConstraints);
		
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 1;
		add(getJTextFieldURL(), gridBagConstraints);
		
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 2;
		add(getJTextFieldLevels(), gridBagConstraints);
		
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 3;
		add(getJTextFieldFormat(), gridBagConstraints);
	}
	
	public JTextField getJTextFieldFormat() {
		if(textFormat == null) {
			textFormat = new JTextField();
		}
		return textFormat;
	}
	
	public JTextField getJTextFieldLevels() {
		if(textLevels == null) {
			textLevels = new JTextField();
		}
		return textLevels;
	}
	
	public JTextField getJTextFieldName() {
		if(textName == null) {
			textName = new JTextField();
		}
		return textName;
	}
	
	public JTextField getJTextFieldURL() {
		if(textURL == null) {
			textURL = new JTextField();
		}
		return textURL;
	}
	
	public boolean check() {
		//return true;
		if(getJTextFieldURL().getText().contains(separator) || 
			getJTextFieldLevels().getText().contains(separator) || 
			getJTextFieldName().getText().contains(separator) || 
			getJTextFieldFormat().getText().contains(separator))
			return false;
		
		if(getJTextFieldURL().getText().startsWith("http://")) {
			try {
				new Integer(getJTextFieldLevels().getText());
			} catch (NumberFormatException e) {
				return false;
			}
			return true;
		}
		return false;
	}

}
